﻿using System.Collections.Generic;
using System.Linq;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;
using Harvest.Api.Types;

namespace Harvest.Api.ExtensionMethods
{

    public static class ContactExtensions
    {

        public static Contact ToContact(this ContactItem entity)
        {
            var result = new Contact
            {

                ClientId = entity.ClientId.HasValue ? entity.ClientId.Value : (int?)null,
                Email = entity.Email,
                Fax = entity.Fax,
                FirstName = entity.FirstName,
                Id = entity.Id.HasValue ? entity.Id.Value : (int?)null,
                LastName = entity.LastName,
                MobilePhone = entity.MobilePhone,
                OfficePhone = entity.OfficePhone
            };
            return result;
        }


        public static ContactItem ToContactItem(this Contact entity)
        {
            var clientId = new NullableInt();
            if (entity.ClientId.HasValue) clientId = entity.ClientId.Value;

            var id = new NullableInt();
            if (entity.Id.HasValue) id = entity.Id.Value;

            var result = new ContactItem
            {
                ClientId = clientId,
                Email = entity.Email,
                Fax = entity.Fax,
                FirstName = entity.FirstName,
                Id = id,
                LastName = entity.LastName,
                MobilePhone = entity.MobilePhone,
                OfficePhone = entity.OfficePhone
            };
            return result;
        }


        public static List<Contact> ToContacts(this IEnumerable<ContactItem> entity)
        {
            var result = entity.Select(x => x.ToContact()).ToList();
            return result;
        }


        public static List<ContactItem> ToContactsItem(this IEnumerable<Contact> entity)
        {
            var result = entity.Select(x => x.ToContactItem()).ToList();
            return result;
        }
    }

}
